package ngat.oss;

import ngat.util.*;
import java.text.*;
import java.util.*;
import java.net.*;

/** ------------------------------------------------------------
 * Phase II Data Entry Tool Configuration and version info.
 *------------------------------------------------------------
 * This class is a Configuration cache to hold details of the current
 * versions of P2T software and resources to allow detection of
 * upgrade requirements when clients log in to the OSS. 
 *
 * $Id: ClientSettings.java,v 1.1 2006/11/27 08:45:00 snf Exp $
 *
 */
public class ClientSettings extends ConfigurationCache {

    public static final int FILTER_DATABASE_VERSION = 0;
    public static final int FACTORY_USER_CONFIG = 1;
    public static final int FACTORY_CONFIG = 2;
    public static final int SOFTWARE_VERSION = 3;
    public static final int HELP_VERSION = 4;
    public static final int RESOURCES_VERSION =5;
    public static final int AUTHOR_NAME = 6;
    public static final int DATE = 7;
    public static final int HELP_URL = 8;

    // Define static cache variables.
    private int filterDatabaseVersion;
    private int factoryUserConfigVersion;
    private int factoryConfigVersion ;
    private int softwareVersion; 
    private int helpVersion;
    private int resourcesVersion;
    private String authorName;
    private Date date;
    private URL helpURL; 

    private SimpleDateFormat sdf;

    public ClientSettings() {
	super();
	essential.put("filter.database.version",          new Integer(FILTER_DATABASE_VERSION));
	essential.put("tool.factory.user_config.version", new Integer(FACTORY_USER_CONFIG));
	essential.put("tool.factory.config.version",      new Integer(FACTORY_CONFIG));
	essential.put("tool.software.version",            new Integer(SOFTWARE_VERSION)); 
	essential.put("tool.help.version",                new Integer(HELP_VERSION));
	essential.put("tool.resources.version",           new Integer(RESOURCES_VERSION));
	essential.put("tool.author",                      new Integer(AUTHOR_NAME));
	essential.put("tool.date",                        new Integer(DATE));
	essential.put("tool.help.url",                    new Integer(HELP_URL));

	sdf = new SimpleDateFormat("dd/MM/yy");

    }

    public void update(String key, String valStr) throws IllegalArgumentException {
	Integer keycode = (Integer)essential.get(key);
	if (keycode == null) throw new IllegalArgumentException("No such key");

	int code = keycode.intValue();

	switch (code) {
	case FILTER_DATABASE_VERSION:
	    try {
		filterDatabaseVersion = getIntValue(key);
	    } catch (ParseException e) {
		throw new IllegalArgumentException("Illegal value for <filter.database.version> "+valStr);
	    }
	    break;
	case FACTORY_USER_CONFIG:
	    try {
		factoryUserConfigVersion = getIntValue(key);
	    } catch (ParseException e) {
		throw new IllegalArgumentException("Illegal value for <factory.user.config.version> "+valStr);
	    }
	    break;
	case FACTORY_CONFIG:
	    try {
		factoryConfigVersion = getIntValue(key);
	    } catch (ParseException e) {
		throw new IllegalArgumentException("Illegal value for <factory.config.version> "+valStr);
	    }
	    break;
	case SOFTWARE_VERSION:
	    try {
		softwareVersion = getIntValue(key);
	    } catch (ParseException e) {
		throw new IllegalArgumentException("Illegal value for <software.version> "+valStr);
	    }
	    break;
	case HELP_VERSION:
	    try {
		helpVersion = getIntValue(key);
	    } catch (ParseException e) {
		throw new IllegalArgumentException("Illegal value for <help.version> "+valStr);
	    }
	    break;
	case RESOURCES_VERSION:
	    try {
		resourcesVersion = getIntValue(key);
	    } catch (ParseException e) {
		throw new IllegalArgumentException("Illegal value for <resources.version> "+valStr);
	    }
	    break;
	case AUTHOR_NAME:
	    authorName = valStr;
	    break;
	case DATE:
	    try {
		date = sdf.parse(valStr);
	    } catch(ParseException e) {
		throw new IllegalArgumentException("Illegal value for <date> "+valStr);
	    }
	    break;
	case HELP_URL:
	    try {
		helpURL = new URL(valStr);
	    } catch (MalformedURLException e) {
		throw new IllegalArgumentException("Illegal value for <help.url> "+valStr);
	    }
	    break;
	default:
	}
    }

    // Public accessors.
    public int getFilterDatabaseVersion()    {return filterDatabaseVersion;}
    public int getFactoryUserConfigVersion() {return factoryUserConfigVersion;}
    public int getFactoryConfigVersion()     {return factoryConfigVersion ;}
    public int getSoftwareVersion()          {return softwareVersion; }
    public int getHelpVersion()              {return helpVersion;}
    public int getResourcesVersion()         {return resourcesVersion;}
    public String getAuthorName()            {return authorName;}
    public Date getDate()                    {return date;}
    public URL getHelpURL()                  {return helpURL; }

    // Public mutators.
    public void setFilterDatabaseVersion(int v)    { this.filterDatabaseVersion = v;}
    public void setFactoryUserConfigVersion(int v) { this.factoryUserConfigVersion = v;}
    public void setFactoryConfigVersion(int v)     { this.factoryConfigVersion = v;}
    public void setSoftwareVersion(int v)          { this.softwareVersion = v;}
    public void setHelpVersion(int v)              { this.helpVersion = v;}
    public void setResourcesVersion(int v)         { this.resourcesVersion = v;}
    public void setAuthorName(String v)            { this.authorName = v;}
    public void setDate(Date v)                    { this.date = v;}
    public void setHelpURL(URL  v)                 { this.helpURL = v;}

}

/** $Log: ClientSettings.java,v $
/** Revision 1.1  2006/11/27 08:45:00  snf
/** Initial revision
/**
/** Revision 1.1  2000/12/12 18:18:32  snf
/** Initial revision
/** */
